package com.atguigu.gmall.mq.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.common.service.RabbitService;
import com.atguigu.gmall.mq.config.DeadLetterMqConfig;
import com.atguigu.gmall.mq.config.DelayedMqConfig;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author atguigu-mqx
 */
@RestController
@RequestMapping("/mq")
public class MqController {

    @Autowired
    private RabbitService rabbitService;

    @Autowired
    private RabbitTemplate rabbitTemplate;


    @GetMapping("sendMsg")
    public Result sendMsg(){
        //  发送消息：
        this.rabbitService.sendMessage("exchange.confirm","routing.confirm6666","来人了，开始接客吧！");

        return Result.ok();
    }

    //  编写控制器发送消息！
    @GetMapping("sendDeadMsg")
    public Result sendDeadMsg(){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //  发送消息： 发送消息的内容改成时间：
        this.rabbitService.sendMessage(DeadLetterMqConfig.exchange_dead,DeadLetterMqConfig.routing_dead_1,"来人了，开始接客吧！");
        //  打印一个时间：
        System.out.println(simpleDateFormat.format(new Date())+":\t 开始发送消息！");
        return Result.ok();
    }

    @GetMapping("sendDelayMsg")
    public Result sendDelayMsg(){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //  延迟消息如何设置?
        rabbitTemplate.convertAndSend(DelayedMqConfig.exchange_delay,DelayedMqConfig.routing_delay,"ok",(msg)->{
            //  设置延迟时间 10秒钟！
            msg.getMessageProperties().setDelay(10000);
            System.out.println("发送时间:\t"+simpleDateFormat.format(new Date()));
            return msg;
        });
        return Result.ok();
    }

}
